package org.test.once.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.test.once.model.Role;
import org.test.once.utils.DBHelper;

public class RoleDao {
	private static RoleDao instance;

	private RoleDao() {
	}

	/**
	 * 获取此类的一个实例
	 * 
	 * @return
	 */
	public static RoleDao getInstance() {
		if (instance == null) {
			instance = new RoleDao();
		}
		return instance;
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @throws SQLException
	 */
	public void save(Role role) throws SQLException {
		String sql = "insert into t_role(name) values(?)";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, role.getName());
		pstmt.execute();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}

	/**
	 * 通过Id查询
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public Role findRoleById(int id) throws SQLException {
		String sql = "select * from t_role where id = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		ResultSet rs = pstmt.executeQuery();
		Role role = null;
		if (rs.next()) {
			role = new Role();
			role.setId(id);
			role.setName(rs.getString("name"));
		}

		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return role;
	}

	/**
	 * 
	 * @param account
	 * @return
	 * @throws SQLException
	 */
//	public Role findUserByAccount(String account) throws SQLException {
//		String sql = "select * from t_user where account = ?";
//
//		Connection conn = DBHelper.getConnection();
//		PreparedStatement pstmt = conn.prepareStatement(sql);
//		pstmt.setString(1, account);
//		ResultSet rs = pstmt.executeQuery();
//		Role role = null;
//		if (rs.next()) {
//			role = new Role();
//			role.setId(rs.getInt("id"));
//			role.setName(rs.getString("name"));
//		}
//
//		DBHelper.close(rs);
//		DBHelper.close(pstmt);
//		DBHelper.close(conn);
//		return role;
//	}

	/**
	 * 查询用户列表
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Role> searchRoles() throws SQLException {
		String sql = "select * from t_role";
		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		ArrayList<Role> roles = new ArrayList<Role>();
		while (rs.next()) {
			Role r = new Role();
			r.setId(rs.getInt("id"));
			r.setName(rs.getString("name"));
			roles.add(r);
		}
		DBHelper.close(rs);
		DBHelper.close(pstmt);
		DBHelper.close(conn);
		return roles;
	}

	/**
	 * 通过id删除用户
	 * 
	 * @param id
	 * @throws SQLException
	 */
	public void deleteRole(int id) throws SQLException {
		String sql = "delete from t_role where id = ?";

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		pstmt.execute();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}

	/**
	 * 更新用户信息
	 * 
	 * @param user
	 * @throws SQLException 
	 */
	public void updateRole(Role role) throws SQLException {
		String sql = "update t_role set name = ? where id=?";
		

		Connection conn = DBHelper.getConnection();
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, role.getName());
		pstmt.setInt(2, role.getId());
		pstmt.execute();

		DBHelper.close(pstmt);
		DBHelper.close(conn);
	}

}
